package com.swt.gap.util;

import com.google.common.collect.Lists;
import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zqw
 * @date 2019/10/25 10:28
 */
@Slf4j
public class CsvReader {

    private CsvParser csvParser;

    public CsvReader(String csvFilePath) {

        File file = new File(csvFilePath);

        String fileCharset = FileCharsetUtil.getFilecharset(file);
        InputStream in;
        CsvParser csvParser = null;
        try {
            in = new FileInputStream(file);
            CsvParserSettings csvParserSettings = new CsvParserSettings();
            csvParserSettings.setLineSeparatorDetectionEnabled(true);
            csvParserSettings.setHeaderExtractionEnabled(true);
            csvParser = new CsvParser(csvParserSettings);
            csvParser.beginParsing(in, fileCharset);

        } catch (IOException e) {
            e.printStackTrace();
        }

        this.csvParser = csvParser;

    }

    public List<String> getHeader() {

        // 表头
        String[] headers = csvParser.getRecordMetadata().headers();
        if (headers != null) {
            return Arrays.asList(headers);
        }

        return new ArrayList<>();

    }

    //读取样板数据
    public List<List<String>> getSamplesWithoutHead(int startIndex, int endIndex) {
        List<List<String>> lists = new ArrayList<>();

        int index = 0;

        String[] headers = csvParser.getRecordMetadata().headers();
        if (headers != null) {
            String[] row;
            while (null != (row = csvParser.parseNext())) {
                index += 1;
                if (row.length == headers.length && index >= startIndex && index < endIndex) {

                    for (int i = 1; i < row.length; i++) {
                        row[i] = row[i] == null ? "" : row[i].trim();
                    }
                    lists.add(Lists.newArrayList(row));
                }

                if (index >= endIndex) {
                    break;
                }
            }
        }

        return lists;
    }

    public List<List<String>> getDataWithoutHead() {
        List<List<String>> lists = new ArrayList<>();

        // 表头
        String[] headers = csvParser.getRecordMetadata().headers();
        if (headers != null) {
            String[] row;
            while (null != (row = csvParser.parseNext())) {
                if (row.length == headers.length) {
                    for (int i = 1; i < row.length; i++) {
                        row[i] = row[i] == null ? "" : row[i].trim();
                    }
                    lists.add(Lists.newArrayList(row));
                }

            }
        }

        return lists;
    }

    public void stopParsing() {
        csvParser.stopParsing();
    }

}
